using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using TVT.UI.Helpers;
using TVT.UI.EventArguments;
using TVT.Common.Helpers;
using TVT.BizLayer.Entities;
using TVT.Common.Exceptions;


namespace TVT.UI.UserControls
{
    public partial class ListAccount : System.Web.UI.UserControl
    {
        public event EventHandler OnEditAccount = null;
        public event EventHandler OnDeletedAccount = null;

        public void DeletedAccount(EventArgs args)
        {
            if (OnDeletedAccount != null)
            {
                OnDeletedAccount(this, args);
            }
        }

        public void EditAccount(EntityEventArgs args)
        {
            if (OnEditAccount != null)
            {
                OnEditAccount(this, args);
            }
        }

        public void Refresh()
        {
            FillGvAccount();
            UPnlListAccount.Update();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillGvAccount();
            }
        }

        protected void FillGvAccount()
        {
            gvAccount.PageSize = Int32.Parse(ConfigurationManager.AppSettings[WellKnownKeys.GridViewsPageSizeKey]);

            List<Account> accounts = Account.FindByUser((Guid)Membership.GetUser().ProviderUserKey);
            gvAccount.DataSource = accounts;
            ViewState[WellKnownKeys.AccountListKey] = accounts;
            gvAccount.DataBind();            
        }


        protected void OnSortingGridViewAccount(object sender, GridViewSortEventArgs e)
        {
            string sort = " DESC";

            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
            else
            {
                if ((SortDirection)ViewState["sortDirection"] == SortDirection.Ascending)
                {
                    ViewState["sortDirection"] = SortDirection.Descending;
                    sort = " DESC";
                }
                else
                {
                    ViewState["sortDirection"] = SortDirection.Ascending;
                    sort = " ASC";
                }
            }

            List<Account> accounts = (List<Account>)ViewState[WellKnownKeys.AccountListKey];
            DataTable dataTable = new DataTable();

            dataTable.Columns.Add("Id");
            dataTable.Columns["Id"].DataType = System.Type.GetType("System.Int32");

            dataTable.Columns.Add("Name");
            dataTable.Columns["Name"].DataType = System.Type.GetType("System.String");

            dataTable.Columns.Add("Interest");
            dataTable.Columns["Interest"].DataType = System.Type.GetType("System.Decimal");

            dataTable.Columns.Add("Company");
            dataTable.Columns["Company"].DataType = System.Type.GetType("System.String");

            DataRow dataRow;

            foreach (Account account in accounts)
            {
                dataRow = dataTable.NewRow();

                dataRow["Id"] = account.Id;
                dataRow["Name"] = account.Name;
                dataRow["Interest"] = account.Interest;
                dataRow["Company"] = account.Company.Name;

                dataTable.Rows.Add(dataRow);
            }

            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);

                dataView.Sort = e.SortExpression + sort;

                gvAccount.DataSource = dataView;
                gvAccount.DataBind();
            }
        }

        protected void gvAccount_PageIndexChanging(Object sender, GridViewPageEventArgs e)
        {
            gvAccount.PageIndex = e.NewPageIndex;
            if (gvAccount.EditIndex != -1)
            {
                e.Cancel = true;
                int newPageNumber = e.NewPageIndex + 1;
            }
            else
            {
                gvAccount.DataSource = ViewState[WellKnownKeys.AccountListKey];
                gvAccount.DataBind();
            }

        }

        protected void gvAccount_DataBound(object sender, EventArgs e)
        {

        }

        protected void gvAccount_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.Header)
            //{
            //    gvAccount.CellSpacing = 0;
            //}
            if (e.Row.RowType != DataControlRowType.Header)
            {
                //gvAccount.CellSpacing = 4;
                if (e.Row.DataItemIndex != -1)
                {
                    int idAccount = 0;
                    StylesApplier stylesApplier = new StylesApplier();

                //    stylesApplier.GridViewStyle(e.Row);

                    if (e.Row.DataItem.GetType() == typeof(Account))
                    {
                        Account account = null;
                        account = ((Account)(e.Row.DataItem));

                        if (account != null)
                            idAccount = account.Id;

                        Label lblAccount = (Label)e.Row.FindControl("lblAccountName");
                        Label lblInterest = (Label)e.Row.FindControl("lblAccountInterest");
                        Label lblCompany = (Label)e.Row.FindControl("lblCompany");

                        account = ((Account)(e.Row.DataItem));
                        lblAccount.Text = account.Name;
                        lblInterest.Text = string.Format(Resources.Decimal4Format, account.Interest.ToString());
                        lblCompany.Text = account.Company.Name;
                       
                    }
                    else
                    {
                        //DataRowView Type
                        DataRowView account = ((DataRowView)(e.Row.DataItem));

                        if (account != null)
                            idAccount = Int32.Parse(account.Row.ItemArray[0].ToString());


                        Label lblAccount = (Label)e.Row.FindControl("lblAccountName");
                        Label lblInterest = (Label)e.Row.FindControl("lblAccountInterest");
                        Label lblCompany = (Label)e.Row.FindControl("lblCompany");

                        lblAccount.Text = account.Row.ItemArray[1].ToString();
                        lblInterest.Text = account.Row.ItemArray[2].ToString();
                        lblCompany.Text = account.Row.ItemArray[3].ToString();
                    }

                    if (idAccount != 0)
                    {
                        ImageButton lbtnEdit = (ImageButton)e.Row.FindControl("lbtnEdit");
                        lbtnEdit.CommandArgument = idAccount.ToString();

                        ImageButton lbtnDelete = (ImageButton)e.Row.FindControl("lbtnDelete");
                        lbtnDelete.CommandArgument = idAccount.ToString();
                    }
                }
            }
        }

        protected void gvAccount_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();

            if (e.CommandName == "EditAccount")
            {
                EntityEventArgs args = new EntityEventArgs();
                args.Id = Int32.Parse(id);
                EditAccount(args);
            }

            if (e.CommandName == "DeleteAccount")
            {
                try
                {
                    Account.Delete(Int32.Parse(id));                   
                    lblAccountSearchResultMessage.Text = Resources.SuccesfullMessage;
                    FillGvAccount();
                    DeletedAccount(new EventArgs());
                    lblAccountSearchResultMessage.Visible = false;
                }
                catch(Exception fex)
                {
                    lblAccountSearchResultMessage.Visible = true;
                    lblAccountSearchResultMessage.Text = fex.Message;
                }
            }
        }
    }

}
